﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using PVAutoTest.TestMngr;
using MoonLight.LibCommon;

namespace PVAutoTest.LibDataBase
{
    class XmlOperator
    {
        public int OpenXml(string path, ref TestSuit testsuit)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(path);
            XmlNodeList elmt_ts = xmldoc.GetElementsByTagName("TestSuit");
            if (elmt_ts == null || elmt_ts.Count == 0) {
                Logger.WriteLog("no testsuit, check TestSuit.xml");
                return -1;
            }
            Logger.WriteLog("elmt_ts is " + elmt_ts[0].ChildNodes.Count);

            foreach (XmlElement elmt_tc in elmt_ts[0].ChildNodes)
            {
                TestCase tc = new TestCase(testsuit);
                tc.GetEvent().case_no = Convert.ToInt32(elmt_tc.GetAttribute("CaseNo"));
                tc.GetEvent().case_sub_no = Convert.ToInt32(elmt_tc.GetAttribute("CaseSubNo"));

                //NOTE: actually there is no need to use foreach,
                //coz there is only one TestData node under each TestCase node,
                //but as there is no direct way to convert XmlNode to XmlElement,
                //so I use foreach to implicit convert it.
                //i know it's ugly but more convenient then to implement a method for it.
                //sorry i'm lazy :(
                foreach (XmlElement elmt_td in elmt_tc.GetElementsByTagName("TestData"))
                {
                    tc.GetEvent().event_id = Convert.ToInt32(elmt_td.GetElementsByTagName("EventId")[0].InnerText);
                    tc.GetEvent().msg_no = elmt_td.GetElementsByTagName("MessageNo")[0].InnerText;
                    tc.GetEvent().dev_id = elmt_td.GetElementsByTagName("DeviceId")[0].InnerText;
                    tc.GetEvent().set_state = elmt_td.GetElementsByTagName("SetState")[0].InnerText;
                    tc.GetEvent().component_id = elmt_td.GetElementsByTagName("ComponentId")[0].InnerText;
                    tc.GetEvent().component_state = elmt_td.GetElementsByTagName("ComponentState")[0].InnerText;
                    tc.GetEvent().event_no = elmt_td.GetElementsByTagName("EventNo")[0].InnerText;
                    tc.GetEvent().msg_str = elmt_td.GetElementsByTagName("MessageString")[0].InnerText;
                }

                foreach (XmlElement elmt_cp in elmt_tc.GetElementsByTagName("CheckPointDB"))
                {
                    foreach (XmlElement elmt in elmt_cp.ChildNodes)
                    {
                        if (elmt.Name == "Event")
                        {
                            tc.GetDBCheckPoints().table_event.DeviceState = Convert.ToBoolean(elmt.GetElementsByTagName("DeviceState")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_event.EventNo = Convert.ToBoolean(elmt.GetElementsByTagName("EventNo")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_event.MessageNo = Convert.ToBoolean(elmt.GetElementsByTagName("MessageNo")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_event.OrgMessage = Convert.ToBoolean(elmt.GetElementsByTagName("OrgMessage")[0].Attributes[0].Value);
                            
                        }
                        else if (elmt.Name == "ComponentEvent")
                        {
                            tc.GetDBCheckPoints().table_componentevent.ComponentId = Convert.ToBoolean(elmt.GetElementsByTagName("ComponentId")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_componentevent.CompState = Convert.ToBoolean(elmt.GetElementsByTagName("CompState")[0].Attributes[0].Value);
                        }
                        else if (elmt.Name == "DeviceFail")
                        {
                            tc.GetDBCheckPoints().table_devicefail.CompFailCount = Convert.ToBoolean(elmt.GetElementsByTagName("CompFailCount")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_devicefail.ComponentId = Convert.ToBoolean(elmt.GetElementsByTagName("ComponentId")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_devicefail.CompState = Convert.ToBoolean(elmt.GetElementsByTagName("CompState")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_devicefail.Ended = Convert.ToBoolean(elmt.GetElementsByTagName("Ended")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_devicefail.EndEventNo = Convert.ToBoolean(elmt.GetElementsByTagName("EndEventNo")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_devicefail.EventNo = Convert.ToBoolean(elmt.GetElementsByTagName("EventNo")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_devicefail.Started = Convert.ToBoolean(elmt.GetElementsByTagName("Started")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_devicefail.State = Convert.ToBoolean(elmt.GetElementsByTagName("State")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_devicefail.StateBit = Convert.ToBoolean(elmt.GetElementsByTagName("StateBit")[0].Attributes[0].Value);
                        }
                        else if (elmt.Name == "DayFail")
                        {
                            tc.GetDBCheckPoints().table_dayfail.ComponentId = Convert.ToBoolean(elmt.GetElementsByTagName("ComponentId")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_dayfail.CompState = Convert.ToBoolean(elmt.GetElementsByTagName("CompState")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_dayfail.Ended = Convert.ToBoolean(elmt.GetElementsByTagName("Ended")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_dayfail.EventNo = Convert.ToBoolean(elmt.GetElementsByTagName("EventNo")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_dayfail.Started = Convert.ToBoolean(elmt.GetElementsByTagName("Started")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_dayfail.State = Convert.ToBoolean(elmt.GetElementsByTagName("State")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_dayfail.StateBit = Convert.ToBoolean(elmt.GetElementsByTagName("StateBit")[0].Attributes[0].Value);
                        }
                        else if (elmt.Name == "MonthFail")
                        {
                            tc.GetDBCheckPoints().table_monthfail.ComponentId = Convert.ToBoolean(elmt.GetElementsByTagName("ComponentId")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_monthfail.TotalTime = Convert.ToBoolean(elmt.GetElementsByTagName("TotalTime")[0].Attributes[0].Value);
                        }
                        else if (elmt.Name == "State")
                        {
                            tc.GetDBCheckPoints().table_state.DeviceState = Convert.ToBoolean(elmt.GetElementsByTagName("DeviceState")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_state.EventCount = Convert.ToBoolean(elmt.GetElementsByTagName("EventCount")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_state.ExtraEventCount = Convert.ToBoolean(elmt.GetElementsByTagName("ExtraEventCount")[0].Attributes[0].Value);
                        }
                        else if (elmt.Name == "EventFrequency")
                        {
                            tc.GetDBCheckPoints().table_eventfrequency.Count = Convert.ToBoolean(elmt.GetElementsByTagName("Count")[0].Attributes[0].Value);
                        }
                        else if (elmt.Name == "OpenEvent")
                        {
                            tc.GetDBCheckPoints().table_openevent.BitNo = Convert.ToBoolean(elmt.GetElementsByTagName("BitNo")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_openevent.ComponentId = Convert.ToBoolean(elmt.GetElementsByTagName("ComponentId")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_openevent.CompState = Convert.ToBoolean(elmt.GetElementsByTagName("CompState")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_openevent.DeviceState = Convert.ToBoolean(elmt.GetElementsByTagName("DeviceState")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_openevent.EventNo = Convert.ToBoolean(elmt.GetElementsByTagName("EventNo")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_openevent.MessageNo = Convert.ToBoolean(elmt.GetElementsByTagName("MessageNo")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_openevent.OrgMessage = Convert.ToBoolean(elmt.GetElementsByTagName("OrgMessage")[0].Attributes[0].Value);
                        }
                        else if (elmt.Name == "GroupState")
                        {
                            tc.GetDBCheckPoints().table_groupstate.EventCount = Convert.ToBoolean(elmt.GetElementsByTagName("EventCount")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_groupstate.State = Convert.ToBoolean(elmt.GetElementsByTagName("State")[0].Attributes[0].Value);
                        }
                        else if (elmt.Name == "ComponentState")
                        {
                            tc.GetDBCheckPoints().table_componentstate.CompState = Convert.ToBoolean(elmt.GetElementsByTagName("CompState")[0].Attributes[0].Value);
                        }
                        else if (elmt.Name == "GroupEvent")
                        {
                            tc.GetDBCheckPoints().table_groupevent.EventNo = Convert.ToBoolean(elmt.GetElementsByTagName("EventNo")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_groupevent.GroupState = Convert.ToBoolean(elmt.GetElementsByTagName("GroupState")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_groupevent.OrgMessage = Convert.ToBoolean(elmt.GetElementsByTagName("OrgMessage")[0].Attributes[0].Value);
                        }
                        else if (elmt.Name == "PmResult")
                        {
                            tc.GetDBCheckPoints().table_pmresult.PmId = Convert.ToBoolean(elmt.GetElementsByTagName("PmId")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_pmresult.Result = Convert.ToBoolean(elmt.GetElementsByTagName("Result")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_pmresult.State = Convert.ToBoolean(elmt.GetElementsByTagName("State")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_pmresult.Step = Convert.ToBoolean(elmt.GetElementsByTagName("Step")[0].Attributes[0].Value);
                            tc.GetDBCheckPoints().table_pmresult.TriggerType = Convert.ToBoolean(elmt.GetElementsByTagName("TriggerType")[0].Attributes[0].Value);
                        }
                    }
                }
                testsuit.AddTestCase(tc);
            }
            
            return 0;
        }

        public int CreateXml(TestSuit testsuit)
        {
            if (testsuit == null || testsuit.GetTestCaseCount() <= 0) {
                Logger.WriteLog("testsuit has no data");
                return -1;
            }

            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            xmldoc.AppendChild(xmldecl);

            XmlElement elmt_ts = xmldoc.CreateElement("", "TestSuit", "");
            xmldoc.AppendChild(elmt_ts);

            int count = testsuit.GetTestCaseCount();
            for (int i = 0; i < count; i++)
            {
                TestData_Event td_event = testsuit.GetTestCaseList()[i].GetEvent();
                XmlElement elmt_temp;

                //TestCase
                XmlElement elmt_tc = elmt_tc = xmldoc.CreateElement("TestCase");
                elmt_tc.SetAttribute("CaseNo", td_event.case_no.ToString());
                elmt_tc.SetAttribute("CaseSubNo", td_event.case_sub_no.ToString());

                #region TestData
                XmlElement elmt_td = elmt_td = xmldoc.CreateElement("TestData");
                elmt_temp = xmldoc.CreateElement("MessageNo");
                elmt_temp.InnerText = td_event.msg_no;
                elmt_td.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("DeviceId");
                elmt_temp.InnerText = td_event.dev_id;
                elmt_td.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("SetState");
                elmt_temp.InnerText = td_event.set_state;
                elmt_td.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("ComponentId");
                elmt_temp.InnerText = td_event.component_id;
                elmt_td.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("ComponentState");
                elmt_temp.InnerText = td_event.component_state;
                elmt_td.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("EventNo");
                elmt_temp.InnerText = td_event.event_no;
                elmt_td.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("MessageString");
                elmt_temp.InnerText = td_event.msg_str;
                elmt_td.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("EventId");
                elmt_temp.InnerText = td_event.event_id.ToString();
                elmt_td.AppendChild(elmt_temp);

                elmt_tc.AppendChild(elmt_td);
                #endregion

                //CheckPointDB
                CheckPoint_DB cp_db = testsuit.GetTestCaseList()[i].GetDBCheckPoints();
                XmlElement elmt_cp_db = xmldoc.CreateElement("CheckPointDB");
                XmlElement elmt_table;

                #region Table Event
                elmt_table = xmldoc.CreateElement("Event");
                elmt_temp = xmldoc.CreateElement("MessageNo");
                elmt_temp.SetAttribute("Value", cp_db.table_event.MessageNo.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("EventNo");
                elmt_temp.SetAttribute("Value", cp_db.table_event.EventNo.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("OrgMessage");
                elmt_temp.SetAttribute("Value", cp_db.table_event.OrgMessage.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("DeviceState");
                elmt_temp.SetAttribute("Value", cp_db.table_event.DeviceState.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                #region Table ComponentEvent
                elmt_table = xmldoc.CreateElement("ComponentEvent");
                elmt_temp = xmldoc.CreateElement("ComponentId");
                elmt_temp.SetAttribute("Value", cp_db.table_componentevent.ComponentId.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("CompState");
                elmt_temp.SetAttribute("Value", cp_db.table_componentevent.CompState.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                #region Table DeviceFail
                elmt_table = xmldoc.CreateElement("DeviceFail");
                elmt_temp = xmldoc.CreateElement("ComponentId");
                elmt_temp.SetAttribute("Value", cp_db.table_devicefail.ComponentId.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("CompFailCount");
                elmt_temp.SetAttribute("Value", cp_db.table_devicefail.CompFailCount.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("Started");
                elmt_temp.SetAttribute("Value", cp_db.table_devicefail.Started.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("Ended");
                elmt_temp.SetAttribute("Value", cp_db.table_devicefail.Ended.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("EventNo");
                elmt_temp.SetAttribute("Value", cp_db.table_devicefail.EventNo.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("State");
                elmt_temp.SetAttribute("Value", cp_db.table_devicefail.State.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("CompState");
                elmt_temp.SetAttribute("Value", cp_db.table_devicefail.CompState.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("StateBit");
                elmt_temp.SetAttribute("Value", cp_db.table_devicefail.StateBit.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("EndEventNo");
                elmt_temp.SetAttribute("Value", cp_db.table_devicefail.EndEventNo.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                #region Table DayFail
                elmt_table = xmldoc.CreateElement("DayFail");
                elmt_temp = xmldoc.CreateElement("Started");
                elmt_temp.SetAttribute("Value", cp_db.table_dayfail.Started.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("ComponentId");
                elmt_temp.SetAttribute("Value", cp_db.table_dayfail.ComponentId.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("Ended");
                elmt_temp.SetAttribute("Value", cp_db.table_dayfail.Ended.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("EventNo");
                elmt_temp.SetAttribute("Value", cp_db.table_dayfail.EventNo.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("State");
                elmt_temp.SetAttribute("Value", cp_db.table_dayfail.State.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("CompState");
                elmt_temp.SetAttribute("Value", cp_db.table_dayfail.CompState.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("StateBit");
                elmt_temp.SetAttribute("Value", cp_db.table_dayfail.StateBit.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                #region MonthFail
                elmt_table = xmldoc.CreateElement("MonthFail");
                elmt_temp = xmldoc.CreateElement("ComponentId");
                elmt_temp.SetAttribute("Value", cp_db.table_monthfail.ComponentId.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("TotalTime");
                elmt_temp.SetAttribute("Value", cp_db.table_monthfail.TotalTime.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                #region Table State
                elmt_table = xmldoc.CreateElement("State");
                elmt_temp = xmldoc.CreateElement("DeviceState");
                elmt_temp.SetAttribute("Value", cp_db.table_state.DeviceState.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("EventCount");
                elmt_temp.SetAttribute("Value", cp_db.table_state.EventCount.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("ExtraEventCount");
                elmt_temp.SetAttribute("Value", cp_db.table_state.ExtraEventCount.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                #region Table EventFrequency
                elmt_table = xmldoc.CreateElement("EventFrequency");
                elmt_temp = xmldoc.CreateElement("Count");
                elmt_temp.SetAttribute("Value", cp_db.table_eventfrequency.Count.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion 

                #region Table OpenEvent
                elmt_table = xmldoc.CreateElement("OpenEvent");
                elmt_temp = xmldoc.CreateElement("ComponentId");
                elmt_temp.SetAttribute("Value", cp_db.table_openevent.ComponentId.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("BitNo");
                elmt_temp.SetAttribute("Value", cp_db.table_openevent.BitNo.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("EventNo");
                elmt_temp.SetAttribute("Value", cp_db.table_openevent.EventNo.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("CompState");
                elmt_temp.SetAttribute("Value", cp_db.table_openevent.CompState.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("MessageNo");
                elmt_temp.SetAttribute("Value", cp_db.table_openevent.MessageNo.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("DeviceState");
                elmt_temp.SetAttribute("Value", cp_db.table_openevent.DeviceState.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("OrgMessage");
                elmt_temp.SetAttribute("Value", cp_db.table_openevent.OrgMessage.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                #region Table GroupState
                elmt_table = xmldoc.CreateElement("GroupState");
                elmt_temp = xmldoc.CreateElement("State");
                elmt_temp.SetAttribute("Value", cp_db.table_groupstate.State.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("EventCount");
                elmt_temp.SetAttribute("Value", cp_db.table_groupstate.EventCount.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                #region Table ComponentState
                elmt_table = xmldoc.CreateElement("ComponentState");
                elmt_temp = xmldoc.CreateElement("CompState");
                elmt_temp.SetAttribute("Value", cp_db.table_componentstate.CompState.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                #region Table GroupEvent
                elmt_table = xmldoc.CreateElement("GroupEvent");
                elmt_temp = xmldoc.CreateElement("EventNo");
                elmt_temp.SetAttribute("Value", cp_db.table_groupevent.EventNo.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("OrgMessage");
                elmt_temp.SetAttribute("Value", cp_db.table_groupevent.OrgMessage.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("GroupState");
                elmt_temp.SetAttribute("Value", cp_db.table_groupevent.GroupState.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                #region Table PmResult
                elmt_table = xmldoc.CreateElement("PmResult");
                elmt_temp = xmldoc.CreateElement("PmId");
                elmt_temp.SetAttribute("Value", cp_db.table_pmresult.PmId.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("Step");
                elmt_temp.SetAttribute("Value", cp_db.table_pmresult.Step.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("TriggerType");
                elmt_temp.SetAttribute("Value", cp_db.table_pmresult.TriggerType.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("Result");
                elmt_temp.SetAttribute("Value", cp_db.table_pmresult.Result.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_temp = xmldoc.CreateElement("State");
                elmt_temp.SetAttribute("Value", cp_db.table_pmresult.State.ToString());
                elmt_table.AppendChild(elmt_temp);

                elmt_cp_db.AppendChild(elmt_table);
                #endregion

                elmt_tc.AppendChild(elmt_cp_db);

                elmt_ts.AppendChild(elmt_tc);
            }

            xmldoc.Save(testsuit.GetSuitDataPath() + "\\TestSuit.xml"); 
            return 0;
        }
    }
}
